﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Model;
using Bll;

namespace 财会系.Admin
{
    public partial class adminAdmin : System.Web.UI.Page
    {
        BllAdmin b = new BllAdmin();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Type"] != null)
                {
                    datashow();
                }
            }
        }

        public void datashow()
        {
            ModelAdmin m = new ModelAdmin();
            if (Session["Admin"] !=null)
            {                
                m.Acc = Session["Admin"].ToString();
            }
            
            DataTable dt = b.All_Information(m);

            gvAdminShow.DataSource = dt;
            gvAdminShow.DataBind();
        }

        protected void gvAdminShow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#777777'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
            }
        }

        protected void gvAdminShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvAdminShow.PageIndex = e.NewPageIndex;
            datashow();
        }

        protected void lbtnCancel_Click(object sender, EventArgs e)
        {
            //取消
            for (var i = 0; i < gvAdminShow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.gvAdminShow.Rows[i].FindControl("chkSelect");
                chkSelect.Checked = false;
            }
        }

        protected void lbtnSelectAll_Click(object sender, EventArgs e)
        {
            //全选
            for (var i = 0; i < gvAdminShow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.gvAdminShow.Rows[i].FindControl("chkSelect");
                chkSelect.Checked = true;
            }
        }

        protected void lbtnReverse_Click(object sender, EventArgs e)
        {
            //反选
            for (var i = 0; i < gvAdminShow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.gvAdminShow.Rows[i].FindControl("chkSelect");
                chkSelect.Checked = !chkSelect.Checked;
            }

        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            bool f = false;
            int n = 0;
            ModelAdmin m = new ModelAdmin();
            for (var i = 0; i < gvAdminShow.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.gvAdminShow.Rows[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    m.AdminId = int.Parse(((Label)gvAdminShow.Rows[i].FindControl("lbladminId")).Text);//获取编号
                    if (m.AdminId != 1)
                    {
                        if (b.Data_Delete(m) == "删除成功！")
                            f = true;
                    }
                    else
                        n = 2;
                }
            }
            if (f)
            {
                Response.Write("<script>confirm('删除成功！')?window.location='./adminAdmin.aspx':window.location='./adminAdmin.aspx';</script>");
                datashow();//重新绑定GridView空间，等于更新界面
            }
            else
            {
                if (n == 2)
                {
                    Response.Write("<script>confirm('超级管理员不可删除！')?window.location='./adminAdmin.aspx':window.location='./adminAdmin.aspx';</script>");
                    datashow();
                }
                else
                {
                    Response.Write("<script>confirm('删除失败！')?window.location='./adminAdmin.aspx':window.location='./adminAdmin.aspx';</script>");
                    datashow();
                }
            }
        }
    }
}